﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DBSchemaViewer.FrontEnd.Main.AppUserControls
{
    public partial class DiagramView : UserControl
    {
        public DiagramView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Use this when the user wants to create or load a new document.
        /// 
        /// This has to be used in conjuntion with the controller calls
        /// to remove all the XML Data structures associated to the current document.
        /// </summary>
        public void WipeChildControls()
        {
            LayoutRoot.Children.Clear();
        }

        public void CheckForScrollBarsVisibility()
        {
            // ScrollViewer --> ScrViewer
            // Canvas       --> LayoutRoot            
            Size LayoutRootScaledSize = GetLayoutCanvasScaledSize();            
            bool isScrollViewXBiggerThanCanvas = (ScrViewer.Width > LayoutRootScaledSize.Width);
            bool isScrollViewYBiggerThanCanvas = (ScrViewer.Height > LayoutRootScaledSize.Height);

            // 1. First check if it's needed to show an scroll bar
            // HORIZONTAL AXIS
            ScrViewer.HorizontalScrollBarVisibility = (isScrollViewXBiggerThanCanvas) ? ScrollBarVisibility.Hidden : ScrollBarVisibility.Visible;
            //  VERTICAL AXIS
            ScrViewer.VerticalScrollBarVisibility = (isScrollViewYBiggerThanCanvas) ? ScrollBarVisibility.Hidden : ScrollBarVisibility.Visible;

            // 2. If we don't need to show an scroll bar, but we have an offset already scrolled
            // check if we have to scroll back in order to show the whole documnet
            // HORIZONTAL AXIS
            AdjustXOffsetBasedOnNewZoomParams(LayoutRootScaledSize);

            //  VERTICAL AXIS
            AdjustYOffsetBasedOnNewZoomParams(LayoutRootScaledSize);
        }

        Size GetLayoutCanvasScaledSize()
        {
            Size scaledSize = new Size();

            scaledSize.Width = LayoutRoot.Width * stCurrentZoom.ScaleX;
            scaledSize.Height = LayoutRoot.Height * stCurrentZoom.ScaleY;

            return scaledSize;
        }

        protected double UnscaleValueX(double value)
        {
            return (value / stCurrentZoom.ScaleX);
        }

        protected double UnscaleValueY(double value)
        {
            return (value / stCurrentZoom.ScaleY);
        }


        /// <summary>
        /// when we zoom out, if there is space on the view port
        /// make the scrollt horizontal offset smaller in order
        /// to show a bigger part of the document.
        /// </summary>
        protected void AdjustXOffsetBasedOnNewZoomParams(Size LayoutRootScaledSize)
        {
            double ScaledHorizontalOffset = ScrViewer.HorizontalOffset * stCurrentZoom.ScaleX;
            double freeHorizontalSpaceInTheViewPort = (ScrViewer.Width - (LayoutRootScaledSize.Width - ScaledHorizontalOffset));

            if (ScrViewer.HorizontalOffset > 0 &&
                freeHorizontalSpaceInTheViewPort > 0
                )
            {
                double newOffsetPosition = (ScrViewer.HorizontalOffset) - UnscaleValueX(freeHorizontalSpaceInTheViewPort);

                if (newOffsetPosition > 0)
                {
                    ScrViewer.ScrollToHorizontalOffset(newOffsetPosition);
                }
                else
                {
                    ScrViewer.ScrollToHorizontalOffset(0);
                }
            }
        }


        /// <summary>
        /// when we zoom out, if there is space on the view port
        /// make the scrollt horizontal offset smaller in order
        /// to show a bigger part of the document.
        /// </summary>
        protected void AdjustYOffsetBasedOnNewZoomParams(Size LayoutRootScaledSize)
        {
            double ScaledVerticalOffset = ScrViewer.VerticalOffset * stCurrentZoom.ScaleY;
            double freeVerticalSpaceInTheViewPort = (ScrViewer.Height - (LayoutRootScaledSize.Height - ScaledVerticalOffset));

            if (ScrViewer.VerticalOffset > 0 &&
                freeVerticalSpaceInTheViewPort > 0
                )
            {
                double newOffsetPosition = (ScrViewer.VerticalOffset) - UnscaleValueY(freeVerticalSpaceInTheViewPort);

                if (newOffsetPosition > 0)
                {
                    ScrViewer.ScrollToVerticalOffset(newOffsetPosition);
                }
                else
                {
                    ScrViewer.ScrollToVerticalOffset(0);
                }
            }
        }


    }
}
